package com.tuoyu.peopletest.view.ac;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.animation.AnimationUtils;

import com.blankj.utilcode.util.LanguageUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.blankj.utilcode.util.SPUtils;
import com.suke.widget.SwitchButton;
import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.model.data.SaveAppData;
import com.tuoyu.peopletest.utils.SCBizUtils;
import com.tuoyu.peopletest.utils.SCLogUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class StartActivity extends NomalBaseActivity implements View.OnClickListener {

    private final static String TAG = "StartActivity";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (!isTaskRoot()
                && getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
                && getIntent().getAction() != null
                && getIntent().getAction().equals(Intent.ACTION_MAIN)) {
            finish();
            return;
        }
        super.onCreate(savedInstanceState);
        //LanguageUtils.updateAppContextLanguage();
    }

    @Override
    public void initContentView() {
        setContentView(R.layout.activity_start);

    }

    @Override
    public void initChildView() {
        PermissionUtils.permission(com.blankj.utilcode.constant.PermissionConstants.CAMERA).request();
        findViewById(R.id.v_btn_start_test).setOnClickListener(this);
        findViewById(R.id.v_btn_start_test_old).setOnClickListener(this);
        findViewById(R.id.v_btn_query).setOnClickListener(this);
        findViewById(R.id.v_btn_start_colloidal_gold_test).setOnClickListener(this);


        GetStoragePermission();
    }

    @Override
    public void initData() {
        super.initData();
        /*Locale locale = LanguageUtils.getAppliedLanguage();

        if (locale != null && locale.getLanguage().equals("zh")) {
            ((SwitchButton) findViewById(R.id.switch_button)).setChecked(false);
        }
        if (locale != null && locale.getLanguage().equals("en")) {
            ((SwitchButton) findViewById(R.id.switch_button)).setChecked(true);
        }*/

        if(SCBizUtils.isZh()){
            ((SwitchButton) findViewById(R.id.switch_button)).setChecked(false);
        }else {
            ((SwitchButton) findViewById(R.id.switch_button)).setChecked(true);
        }
    }

    @Override
    public void initListener() {
        super.initListener();

        ((SwitchButton) findViewById(R.id.switch_button)).setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                if (isChecked) {
                    LanguageUtils.applyLanguage(Locale.ENGLISH);
                } else {
                    LanguageUtils.applyLanguage(Locale.SIMPLIFIED_CHINESE);
                }
            }
        });
    } 

    private void GetStoragePermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            int REQUEST_CODE_CONTACT = 101;
            String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
//验证是否许可权限
            for (String str : permissions) {
                if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
                    this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
                    return;
                }
            }
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    /**
     * 单一任务模式
     *
     * @param intent
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        SCLogUtils.e(TAG, "onNewIntent=" + intent.toString());
        setIntent(intent);
    }

    @Override
    public void onBackPressed() {

        super.onBackPressed();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    public String getDateString() {
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        String formatString = "yyyyMMddHHmmss";
        SimpleDateFormat sdf = new SimpleDateFormat(formatString, Locale.ENGLISH);
        return sdf.format(c.getTime());
    }

    public File getSaveFile() {
        File file = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            file = Environment.getExternalStorageDirectory();
        } else {
            file = getFilesDir();
        }

//        if(file!=null){
//            file = new File(file,"stresscube");
//            if(!file.exists()){
//                file.mkdir();
//            }
//            file = new File(file,"scantestpd");
//            if(!file.exists()){
//                file.mkdir();
//            }
//        }

        return file;
    }

    private String getDataFilename() {
        return getSaveFile() + "/" + getDateString() + ".txt";
    }

    private boolean SaveFile() {
        try {
            String resultFileName = getDataFilename();
            FileOutputStream out = new FileOutputStream(resultFileName);
            out.write(("保存\r\n").getBytes());
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.v_btn_start_test:
                //判断是否存在,为测试完毕的
                //开始测试
                startActivity(new Intent(this, MainNewActivity.class));
                break;
            case R.id.v_btn_start_test_old:
                //判断是否存在,为测试完毕的
                //开始测试
//                SaveFile();

                startActivity(new Intent(this, SettingActivity.class));
                break;
            case R.id.v_btn_query:
                //查询
                startActivity(new Intent(this, QueryListActivity.class));
                break;
            case R.id.v_btn_start_colloidal_gold_test:
                //胶体金测试
                startActivity(new Intent(this, ColloidalGoldActivity.class));
                break;


        }
    }

}